package com.example.studykotlin.kotlin_actual_combat.art.window

import android.content.Context
import android.graphics.PixelFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.Button
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.studykotlin.Path
import com.example.studykotlin.R
import com.example.studykotlin.databinding.ActivityWindowBinding
import com.example.studykotlin.kotlin_actual_combat.BaseActivity.BaseActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.safframework.log.L
import java.lang.Exception
import java.util.logging.Logger

@Route(path = Path.window)
class WindowActivity : BaseActivity(), View.OnTouchListener{
    private lateinit var binding: ActivityWindowBinding
    private lateinit var mWindowManager: WindowManager
    private lateinit var floatingButton: Button
    private lateinit var params: WindowManager.LayoutParams

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWindowBinding.inflate(layoutInflater)
        setContentView(binding.root)
        checkPermissions()
        initView()
        initEvent()
    }

    private fun initView() {
        mWindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    private fun initEvent() {
        binding.btnCreateWindow.setOnClickListener {
            try {
                floatingButton = Button(this)
                floatingButton.text = "button"
                floatingButton.isAllCaps = false
                params = WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,0,0,PixelFormat.TRANSPARENT)
                params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                params.gravity = Gravity.LEFT or Gravity.TOP
                params.type = 1539
                params.x = 100
                params.y = 300
                floatingButton.setOnTouchListener(this)
                mWindowManager.addView(floatingButton,params)
            } catch (e : Exception){
                e.printStackTrace()
            }
        }
    }

    private fun checkPermissions() {
        XXPermissions.with(this)
            .permission(Permission.SYSTEM_ALERT_WINDOW)
            .request { permissions, all ->
                L.i("get window permission success.")
            }
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        var x = event?.rawX
        var y = event?.rawY
        if(event?.action == MotionEvent.ACTION_MOVE) {
            params.x = x!!.toInt()
            params.y = y!!.toInt()
            mWindowManager.updateViewLayout(floatingButton,params)
        }
        return false
    }
}